﻿
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.IO;
using System.Xml;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
using Swaf.Security;

using UserLogin;

namespace Articles
{
    public class ArtFormBizRules : Syrinx.BaseBizRules
    {
		protected static Regex s_fieldNameRegex = new Regex("^([^_]+)_([^_]+)(?:_(.+))?");
        public IBizObjList getUserArtFormResponses(IApplication app, IRuleContext context, Swaf.BGuid.Guid articleId)
        {
            context.put("companyId", ActiveCompanyId);
            IBizObjList responses = app.appData.get("DA.Articles.GetArtFormUserResponses", context) as IBizObjList;
            return responses;
        }

        public void deleteUserArtFormResponse(IApplication app, IRuleContext context, Swaf.BGuid.Guid responseId)
        {
            context.put("id", responseId);
            app.appData.put("DA.Articles.DeleteArtFormUserResponse", context);
        }

        public List<string> updateUserArtFormData(IApplication app, IRuleContext context, IBizObj artFormData)
        {
			List<string> failures = null;
			Dictionary<string,XmlAttribute> replacements = null;
			XmlDocument formData = new XmlDocument();
			formData.LoadXml((string)artFormData.get("formData"));
			IClassFactory validationFactory = app.factories["ArtFormValidators"];
			foreach (XmlAttribute attrib in formData.DocumentElement.Attributes)
			{
				Match m = s_fieldNameRegex.Match(attrib.Name);
				if (m.Success)
				{
					string realName = m.Groups[1].Value;
					string validatorName = m.Groups[2].Value;
					string validatorInfo = m.Groups[3].Value;
					IFormFieldValidator validator = validationFactory.createFromNickName(validatorName) as IFormFieldValidator;
					if (validator != null)
					{
						string failureInfo = validator.validate(attrib.Value, validatorInfo);
						if (failureInfo != null)
						{
							if (failures == null)
								failures = new List<string>();
							failures.Add(failureInfo);
						}
						if (replacements == null)
							replacements = new Dictionary<string,XmlAttribute>();
						replacements[realName] = attrib;
					}
				}
			}

			if (failures == null)
			{
				if (replacements != null)
				{
					foreach (KeyValuePair<string, XmlAttribute> attrib in replacements)
					{
						formData.DocumentElement.Attributes.Remove(attrib.Value);
						formData.DocumentElement.SetAttribute(attrib.Key, attrib.Value.Value);
					}
					artFormData.put("formData", formData.DocumentElement.OuterXml);
				}
				artFormData.put("user", CurrentUser);
				artFormData.put("lastUpdated", DateTime.UtcNow);
				app.appData.put("DA.Articles.AddArticleFormUserResponse", artFormData);
				context.RuleGroup.execute("notifyObservers", null, "", new object[] {
						"ArtFormDataSubmited", artFormData});
			}

			return failures;
        }

        public IBizObj getUserArtFormData(IApplication app, IRuleContext context, object articleId)
        {
            return null;
        }
    }
}
